package 标准接口;



import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;

import java.util.HashMap;
import java.util.Map;


public class CreateRequestinfo2057 {

//第一步做一次就行了  记录下来
    /**
     * 模拟缓存服务
     */
    private static final Map<String,String> SYSTEM_CACHE = new HashMap <>();

    /**
     * ecology 系统发放的授权许可证(appid)
     */
    private static final String APPID = "EEAA5436-7577-4BE0-8C6C-89E9D44801EA";


    public static void main(String[] args) {
        HttpRequest.closeCookie();
        testRestful("http://oa.maxwell-gp.com.cn:8090","/api/workflow/paService/doCreateRequest","");
    }
    /**
     * 第一步：
     *
     * 调用 ecology 注册接口,根据 appid 进行注册,将返回服务端公钥和 Secret 信息
     */

    public static Map<String,Object> testGetoken(String address){
        // 从系统缓存或者数据库中获取 ECOLOGY 系统公钥和 Secret 信息

        String secret ="6a393956-42c1-4b75-8b77-91c342ed90f1";
        String spk = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCmIqeh3HlAxMNSjepuY1YsRj6pqfX/r88v1xOKI7+vIXDpMLgmaA8ytRGTyHaxscXiXxcCKE6T+z54qrxaxw6b4/WSfcH8QaghHhQRAtvMWxP/SC1Vg5dniQMUVvrsB2wbf0DfyiftGeBf8gJbe1uWsXrMZLZE3olrDMCjylo8SwIDAQAB";

        SYSTEM_CACHE.put("SERVER_PUBLIC_KEY",spk);
        //ECOLOGY 返回的系统密钥
        SYSTEM_CACHE.put("SERVER_SECRET",secret);

        RSA rsa = new RSA(null,spk);
        //对秘钥进行加密传输，防止篡改数据
        String encryptSecret = rsa.encryptBase64(secret,CharsetUtil.CHARSET_UTF_8,KeyType.PublicKey);
        //调用 ECOLOGY 系统接口进行注册
        String data = HttpRequest.post(address+ "/api/ec/dev/auth/applytoken")
                .header("appid",APPID)
                .header("secret",encryptSecret)
                .header("time","3600")
                .execute().body();
        System.out.println("testGetoken()："+data);
        Map<String,Object> datas = JSONUtil.parseObj(data);
        //ECOLOGY 返回的 token
        // TODO 为 Token 缓存设置过期时间
        SYSTEM_CACHE.put("SERVER_TOKEN",StrUtil.nullToEmpty((String)datas.get("token")));
        return datas;
    }

    public static String testRestful(String address,String api,String jsonParams){
        //ECOLOGY 返回的 token
        String token=(String) testGetoken(address).get("token");
        String spk = SYSTEM_CACHE.get("SERVER_PUBLIC_KEY");

        //封装请求头参数
        RSA rsa = new RSA(null,spk);
        //对用户信息进行加密传输,暂仅支持传输 OA 用户 ID
        String encryptUserid = rsa.encryptBase64("243",CharsetUtil.CHARSET_UTF_8,KeyType.PublicKey);
        //调用 ECOLOGY 系统接口
        String data = HttpRequest.post(address + api)
                .header("appid",APPID)
                .header("token",token)
                .header("userid",encryptUserid)
                .body(
                        "workflowId=2057&requestName=测试马上删&"+
                                "mainData=["
                                + "{'fieldName':'text_no','fieldValue':'QR-TS-03'},"
                                + "{'fieldName':'state','fieldValue':'A/1'},"
                                + "{'fieldName':'sx_date','fieldValue':'2019/10/30'},"
                                + "{'fieldName':'kh_name','fieldValue':'客户名称'},"
                                + "{'fieldName':'tbr','fieldValue':'243'},"
                                + "{'fieldName':'kd_date','fieldValue':'2024-09-23'},"//开单日期
                                + "{'fieldName':'jx_name','fieldValue':'机型名称'},"
                                + "{'fieldName':'ys_date','fieldValue':'2024-09-19'},"//验收日期
                                + "{'fieldName':'jx_no','fieldValue':'机型编号'},"
                                + "{'fieldName':'guoneiwai','fieldValue':'0'},"//国内国外
                                + "{'fieldName':'xqwllb','fieldValue':'0'},"//需求物料类别
                                + "{'fieldName':'sbzt','fieldValue':'0'},"//设备状态
                                + "{'fieldName':'miaoshu','fieldValue':'改造内容描述'},"
                                + "{'fieldName':'futu','fieldValue':[" +
                                "{'fileName':'微信图片_20240902143441.jpg','filePath':'https://plt.maxwell-gp.com.cn:8056/galaxy/maxwell-review-server/review/api/file/previewImg?filePath=mwgroup/M00/00/5E/wKgAg2bVYAKADO1jAAKeR6317xo487.jpg'}" +
                                "]},"
                                + "{'fieldName':'yuanyin','fieldValue':'申请改造原因'},"
                                + "{'fieldName':'zuzhang','fieldValue':'243'},"//选择组长审批
                                + "{'fieldName':'yfcpxjl','fieldValue':'243'},"//研发产品线经理
                                + "{'fieldName':'jh_date','fieldValue':'2024-10-01'},"//计划改造时间
                                + "{'fieldName':'zcfzr','fieldValue':'243'},"//驻厂负责人
                                + "{'fieldName':'yfcpxjl','fieldValue':'0'}"//研发产品线经理
                                + "]"

                )

                .execute().body();

        System.out.println("token:"+token);
        System.out.println("userid:"+encryptUserid);
        System.out.println("testRestful()："+data);

        return data;
    }



}
